package com.server.controller;

import com.server.pojo.Users;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author bigFish
 * @Title: public:
 */
@Log4j2
@RestController
@RequestMapping("/v1")
public class User {
    //    获取一个执行SQL的对象
    private final SqlSessionTemplate template;


    public User(SqlSessionTemplate template) {
        this.template = template;
    }


    @ApiOperation("获取用户人数")
    @GetMapping("/getUserCount")
    public int getUsers() {
        return  template.selectOne("getUser");
    }

    @ApiOperation("获取人数")
    @GetMapping("/getUserList")
    public List getUserList() {
        return template.selectList("getUsers");
    }

    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public String addUser(@RequestBody Users users) {
        int insert = template.insert("addUser",users);
        if (insert==1){
            return "添加成功 success";
        }
        return "添加失败 fail";
    }

    @ApiOperation("更新用户")
    @PutMapping("/upUser")
    public String upUser(@RequestBody Users users) {
        int insert = template.update("upUser",users);
        if (insert==1){
            return "更新成功 success";
        }
        return "更新失败 fail";
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/dlUser/{id}")
    public String dlUser(@PathVariable int id) {
        int insert = template.delete("dlUser",id);
        if (insert==1){
            return "删除成功 success";
        }
        return "删除失败 fail";
    }


}
